<template>
  <div class="layout-padding">
    <div class="layout-padding-auto layout-padding-view">
      <el-row v-show="showSearch">
        <el-form :model="state.queryForm" ref="queryRef" :inline="true" @keyup.enter="getDataList">
      <el-form-item :label="$t('CoalFileInfo.userInfoId')" prop="userInfoId" >
        <el-input :placeholder="t('CoalFileInfo.inputUserInfoIdTip')" v-model="state.queryForm.userInfoId"
          style="max-width: 180px" />
      </el-form-item>
      <el-form-item :label="$t('CoalFileInfo.invitationId')" prop="invitationId" >
        <el-input :placeholder="t('CoalFileInfo.inputInvitationIdTip')" v-model="state.queryForm.invitationId"
          style="max-width: 180px" />
      </el-form-item>
      <el-form-item :label="$t('CoalFileInfo.profileId')" prop="profileId" >
        <el-input :placeholder="t('CoalFileInfo.inputProfileIdTip')" v-model="state.queryForm.profileId"
          style="max-width: 180px" />
      </el-form-item>
      <el-form-item :label="$t('CoalFileInfo.fileUrl')" prop="fileUrl" >
        <el-input :placeholder="t('CoalFileInfo.inputFileUrlTip')" v-model="state.queryForm.fileUrl"
          style="max-width: 180px" />
      </el-form-item>
      <el-form-item :label="$t('CoalFileInfo.fileType')" prop="fileType" >
        <el-input :placeholder="t('CoalFileInfo.inputFileTypeTip')" v-model="state.queryForm.fileType"
          style="max-width: 180px" />
      </el-form-item>
      <el-form-item :label="$t('CoalFileInfo.filePath')" prop="filePath" >
        <el-input :placeholder="t('CoalFileInfo.inputFilePathTip')" v-model="state.queryForm.filePath"
          style="max-width: 180px" />
      </el-form-item>
      <el-form-item :label="$t('CoalFileInfo.fileName')" prop="fileName" >
        <el-input :placeholder="t('CoalFileInfo.inputFileNameTip')" v-model="state.queryForm.fileName"
          style="max-width: 180px" />
      </el-form-item>
      <el-form-item :label="$t('CoalFileInfo.createDate')" prop="createDate" >
        <el-input :placeholder="t('CoalFileInfo.inputCreateDateTip')" v-model="state.queryForm.createDate"
          style="max-width: 180px" />
      </el-form-item>
      <el-form-item :label="$t('CoalFileInfo.sort')" prop="sort" >
        <el-input :placeholder="t('CoalFileInfo.inputSortTip')" v-model="state.queryForm.sort"
          style="max-width: 180px" />
      </el-form-item>
          <el-form-item>
            <el-button formDialogRef icon="search" type="primary" @click="getDataList">
              {{ $t('common.queryBtn') }}
            </el-button>
            <el-button icon="Refresh" formDialogRef @click="resetQuery">{{ $t('common.resetBtn') }}</el-button>
          </el-form-item>
        </el-form>
      </el-row>
      <el-row>
        <div class="mb8" style="width: 100%">
          <el-button formDialogRef icon="folder-add" type="primary" class="ml10" @click="formDialogRef.openDialog()"
            v-auth="'coalSystem_CoalFileInfo_add'">
            {{ $t('common.addBtn') }}
          </el-button>
          <el-button plain formDialogRef :disabled="multiple" icon="Delete" type="primary" class="ml10"
            v-auth="'coalSystem_CoalFileInfo_del'" @click="handleDelete(selectObjs)">
            {{ $t('common.delBtn') }}
          </el-button>
          <right-toolbar v-model:showSearch="showSearch" 	:export="'coalSystem_CoalFileInfo_export'"
						@exportExcel="exportExcel" class="ml10" style="float: right;margin-right: 20px"
            @queryTable="getDataList"></right-toolbar>
        </div>
      </el-row>
      <el-table :data="state.dataList" v-loading="state.loading" style="width: 100%"
        @selection-change="handleSelectionChange" @sort-change="sortChangeHandle">
        <el-table-column type="selection" width="40" align="center" />
        <el-table-column type="index" :label="t('CoalFileInfo.index')" width="40" />
          <el-table-column prop="userInfoId" :label="t('CoalFileInfo.userInfoId')" sortable="custom" show-overflow-tooltip/>
          <el-table-column prop="invitationId" :label="t('CoalFileInfo.invitationId')" sortable="custom" show-overflow-tooltip/>
          <el-table-column prop="profileId" :label="t('CoalFileInfo.profileId')" sortable="custom" show-overflow-tooltip/>
          <el-table-column prop="fileUrl" :label="t('CoalFileInfo.fileUrl')"  show-overflow-tooltip/>
          <el-table-column prop="fileType" :label="t('CoalFileInfo.fileType')" sortable="custom" show-overflow-tooltip/>
          <el-table-column prop="filePath" :label="t('CoalFileInfo.filePath')"  show-overflow-tooltip/>
          <el-table-column prop="fileName" :label="t('CoalFileInfo.fileName')"  show-overflow-tooltip/>
          <el-table-column prop="createDate" :label="t('CoalFileInfo.createDate')"  show-overflow-tooltip/>
          <el-table-column prop="sort" :label="t('CoalFileInfo.sort')" sortable="custom" show-overflow-tooltip/>
        <el-table-column :label="$t('common.action')" width="150">
          <template #default="scope">
            <el-button icon="edit-pen" text type="primary" v-auth="'coalSystem_CoalFileInfo_edit'"
              @click="formDialogRef.openDialog(scope.row.fileId)">{{ $t('common.editBtn') }}</el-button>
            <el-button icon="delete" text type="primary" v-auth="'coalSystem_CoalFileInfo_del'" @click="handleDelete([scope.row.fileId])">{{
              $t('common.delBtn')
            }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination" />
    </div>

    <!-- 编辑、新增  -->
    <form-dialog ref="formDialogRef" @refresh="getDataList(false)" />
  </div>
</template>

<script setup lang="ts" name="systemCoalFileInfo">
import { BasicTableProps, useTable } from "/@/hooks/table";
import { fetchList, delObjs } from "/@/api/coalSystem/CoalFileInfo";
import { useMessage, useMessageBox } from "/@/hooks/message";
import { useDict } from '/@/hooks/dict';
import { useI18n } from "vue-i18n";

// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));
const { t } = useI18n()
// 定义查询字典

// 定义变量内容
const formDialogRef = ref()
// 搜索变量
const queryRef = ref()
const showSearch = ref(true)
// 多选变量
const selectObjs = ref([]) as any
const multiple = ref(true)

const state: BasicTableProps = reactive<BasicTableProps>({
  queryForm: {},
  pageList: fetchList
})

//  table hook
const {
  getDataList,
  currentChangeHandle,
  sizeChangeHandle,
  sortChangeHandle,
  downBlobFile
} = useTable(state)

// 清空搜索条件
const resetQuery = () => {
  // 清空搜索条件
  queryRef.value?.resetFields()
  // 清空多选
  selectObjs.value = []
  getDataList()
}

// 导出excel
const exportExcel = () => {
  downBlobFile('/coalSystem/CoalFileInfo/export', state.queryForm, 'CoalFileInfo.xlsx')
}

// 多选事件
const handleSelectionChange = (objs: any) => {
	selectObjs.value.push(...objs.map((val: any) => val.fileId));
	multiple.value = !objs.length;
};

// 删除操作
const handleDelete = async (ids: string[]) => {
	try {
		await useMessageBox().confirm(t('common.delConfirmText'));
	} catch {
		return;
	}

	try {
		await delObj(ids);
		getDataList();
		useMessage().success(t('common.delSuccessText'));
	} catch (err: any) {
		useMessage().error(err.msg);
	}
};
</script>